package edu.vt.ece_3574.amber_alert.curfew;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import edu.vt.ece_3574.amber_alert.PrimaryMain;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.membership.MemberAccountSettings;
import edu.vt.ece_3574.amber_alert.membership.PrimaryAdderActivity;

/**
 * This class handles getting the selected time and location for a curfew
 * 	and registering an AlarmManager to start the CurfewService at the specified time
 * 
 * @author acorvin
 */
public class CurfewCreationActivity extends MapActivity {
	private CurfewLocationSelectorHandler locationSelectorHandler;
	private TimePicker timePicker;
	private Calendar calendar = Calendar.getInstance();
	private PendingIntent pendingIntent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.curfew_creation);
		
		locationSelectorHandler = new CurfewLocationSelectorHandler((MapView) findViewById(R.id.mapView), getResources());

		//Get the TimePicker object so that its currently displayed time can be set
		timePicker = (TimePicker) findViewById(R.id.selectCurfewTime);
		
		//Get the current hour and minute
		int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
		int currentMinute = calendar.get(Calendar.MINUTE);
		
		//Sets the current time to one minute from now, which will be displayed on the TimePicker
		//If the current time is the last minute in an hour or the last hour in a day, this is handled
		if (currentMinute == 59) {
			currentMinute = 0;
			if (currentHour == 23) {
				currentHour = 0;
			}
			else {
				currentHour++;
			}
		}
		else {
			currentMinute++;
		}
		
		//Set the time displayed on the time picker to the current time plus one minute
		timePicker.setCurrentHour(currentHour);
		timePicker.setCurrentMinute(currentMinute);
		
		Button setCurfewButton =  (Button) findViewById(R.id.setCurfewButton);
		setCurfewButton.setOnClickListener(setButtonListener);
		
		Button backButton = (Button) findViewById(R.id.curfewBackButton);
		backButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(CurfewCreationActivity.this, PrimaryMain.class);
				startActivity(i);
				CurfewCreationActivity.this.finish();
			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	private View.OnClickListener setButtonListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			//Get the time selected on the TimePicker
			int selectedHour = timePicker.getCurrentHour();
			int selectedMinute = timePicker.getCurrentMinute();
			
			//Get the location selected on the map
			GeoPoint curfewLocation = locationSelectorHandler.getSelectedLocation();
			
			//Set the curfew location of the selected account to the location chosen on the map
			PrimaryMain.selectedMember.setCurfewLocation(curfewLocation);
			
			//Create an intent and service for the CurfewService, which will run in the background
			Intent serviceIntent = new Intent(CurfewCreationActivity.this, CurfewService.class);
			pendingIntent = PendingIntent.getService(CurfewCreationActivity.this, 12345, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
			
			//Create an AlarmManager to handle when the curfew service should be started
			AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
			
			//Create another calendar. Set its current time to the time selected in the TimePicker
			Calendar cal = new GregorianCalendar();
			cal.set(Calendar.HOUR_OF_DAY, selectedHour);
			cal.set(Calendar.MINUTE, selectedMinute);
			cal.set(Calendar.SECOND, 0);
			
			//Get the difference between the current time and the desired time for the curfew
			long desiredMillis = cal.getTimeInMillis();
			long currentMillis = System.currentTimeMillis();
			long millisDifference = desiredMillis - currentMillis;
			
			//If the time for the curfew has already passed, set it to the same time one day forward in time (there are 86400000 milliseconds in a day)
			if (millisDifference <= 0) {
				millisDifference += 86400000;
			}
			
			//Set the AlarmManager to start the CurfewService at the specified time
			alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + millisDifference, pendingIntent);
			
			//Return back to the primary screen after setting up the curfew
			Intent i = new Intent(CurfewCreationActivity.this, PrimaryMain.class);
			startActivity(i);
			CurfewCreationActivity.this.finish();
		}
	};
	public void onBackPressed() { 
		Intent i = new Intent(CurfewCreationActivity.this, PrimaryMain.class);
		startActivity(i);
		finish();
		return; 
	}
}
